package com.tanhua.server.controller;

import com.tanhua.server.service.SoundService;
import com.tanhua.server.vo.SoundVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@Slf4j
public class SoundController {
    @Autowired
    private SoundService soundService;

    @PostMapping("peachblossom")
    public ResponseEntity<Void> saveSound(@RequestParam("soundFile") MultipartFile soundFile){
        try {
            //根据返回值判断是否保存成功
            boolean bool = this.soundService.saveSound(soundFile);
            if (!bool){
                return ResponseEntity.ok(null);
            }
        } catch (Exception e) {
            log.error("传音失败! " + e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    @GetMapping("peachblossom")
    public ResponseEntity<SoundVo> getSound() throws Exception {
        SoundVo soundVo = this.soundService.querySound();
        if (null == soundVo){
            throw new Exception("今日次数已用完~~~");
        }
        return ResponseEntity.ok(soundVo);
    }
}
